//
//  THImageAlertContentView.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THImageAlertContentView.h"

@interface THImageAlertContentView ()

@property (nonatomic, strong)UIImageView *bgImgV;
@property (nonatomic, strong)UIImageView *topImgV;

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)YYLabel *descLabel;
@property (nonatomic, strong)UIButton *sureBtn;
@property (nonatomic, strong)UIButton *cancelBtn;

@property (nonatomic, strong)UIButton *closeBtn;

@end

@implementation THImageAlertContentView

#pragma mark - 子视图配置
/// 配置子控件
- (void)configViews
{
    UIView *lastView;
    
    self.layer.cornerRadius = kSizeRateTo(20);
    self.layer.masksToBounds = YES;
    
    [self addSubview:self.bgImgV];
    [self.bgImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_offset(0);
        make.height.offset(kSizeRateTo(120));
    }];
     
    // 标题
    [self addSubview:self.titleLabel];
    self.titleLabel.text = self.title;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kSizeRateTo(20));
        make.centerX.equalTo(self);
        make.left.greaterThanOrEqualTo(kSizeRateTo(30));
    }];
    lastView = self.titleLabel;

    // 内容
    [self addSubview:self.descLabel];
    if (self.desc) {
        self.descLabel.text = self.desc;
    }
    if (self.attributedString) {
        self.descLabel.attributedText = self.attributedString;
    }
    
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).offset(kSizeRateTo(10));
        make.left.equalTo(kSizeRateTo(20));
        make.centerX.equalTo(0);
    }];
    lastView = self.descLabel;
    
   // 图片尺寸
    CGSize size = [self.iconStr zh_imageUrlSize];
    if (size.height == 0) {
        // 没有图片
        if ([THBaseUtil isBlankString:self.iconStr]) {
            size = CGSizeZero;
        } else {
            // 默认图片
            size = CGSizeMake(kSizeRateTo(140), kSizeRateTo(140));
        }
    }
    
    // 图片
    [self addSubview:self.topImgV];
    if ([self.iconStr containsString:@"/"]) {
  
        [self.topImgV sd_setImageWithURL:[NSURL URLWithString:self.iconStr] placeholderImage:nil];

    }else{
        self.topImgV.image = kImage(self.iconStr);
    }
    
    [self.topImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).offset(kSizeRateTo(20));
        make.centerX.equalTo(0);
        make.height.mas_equalTo(size.height);
        make.left.equalTo(0);

    }];
    lastView = self.topImgV;
    
    // 确定按钮
    [self.sureBtn setTitle:self.sureBtnTitle forState:UIControlStateNormal];
    [self addSubview:self.sureBtn];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kSizeRateTo(240), kSizeRateTo(46)));
        make.top.equalTo(lastView.mas_bottom).offset(kSizeRateTo(20));
        make.centerX.equalTo(0);
    }];
    [_sureBtn zh_roundedCornerWithRadius:kSizeRateTo(23)];

    // 取消
    if (self.cancelBtnTitle.length > 0) {
        [self.cancelBtn setTitle:self.cancelBtnTitle forState:UIControlStateNormal];
        [self addSubview:self.cancelBtn];
        
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(kSizeRateTo(240), kSizeRateTo(20)));
            make.top.equalTo(self.sureBtn.mas_bottom).offset(kSizeRateTo(12));
            make.centerX.equalTo(0);
            make.bottom.equalTo(-kSizeRateTo(20));
        }];
    }else{
        [self.sureBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(kSizeRateTo(-20));
        }];
    }
}

#pragma mark - btn event

- (void)onCancelBtnClick:(UIButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(zh_alertContentViewDelegate_cancelEvent)]) {
        [self.delegate zh_alertContentViewDelegate_cancelEvent];
    }
}

- (void)onSureBtnClick:(UIButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(zh_alertContentViewDelegate_sureEvent)]) {
        [self.delegate zh_alertContentViewDelegate_sureEvent];
    }
}

#pragma mark - lazy
-(UIImageView *)bgImgV{
    if(!_bgImgV){
        _bgImgV = [[UIImageView alloc]init];
        _bgImgV.image = kImage(@"baseImg_common_image_alert_bg");
    }
    return _bgImgV;
}
- (UIImageView *)topImgV
{
    if(!_topImgV)
    {
        _topImgV=[[UIImageView alloc]init];
        _topImgV.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _topImgV;
}

- (UILabel *)titleLabel
{
    if(!_titleLabel)
    {
        _titleLabel=[[UILabel alloc]initWithFontName:FONT_MEDIUM size:kSizeRateTo(18) textColor:Color_text_33];
        _titleLabel.numberOfLines = 0;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (YYLabel *)descLabel
{
    if(!_descLabel)
    {
        _descLabel=[[YYLabel alloc]init];
        _descLabel.font = kFontRegular(kSizeRateTo(15));
        _descLabel.textColor = Color_text_33;
        _descLabel.numberOfLines = 0;
        _descLabel.preferredMaxLayoutWidth = kSizeRateTo(240);
        _descLabel.textAlignment = NSTextAlignmentCenter;
        _descLabel.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return _descLabel;
}

- (UIButton *)cancelBtn
{
    if(!_cancelBtn)
    {
        _cancelBtn = [[UIButton alloc]init];
        _cancelBtn.titleLabel.font = kFontRegular(kSizeRateTo(14));
        [_cancelBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(onCancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIButton *)sureBtn
{
    if(!_sureBtn)
    {
        _sureBtn=[[UIButton alloc]init];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = kFontMedium(kSizeRateTo(16));
        [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _sureBtn.backgroundColor = kThemeColor;
        [_sureBtn addTarget:self action:@selector(onSureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

- (UIButton *)closeBtn
{
    if (!_closeBtn) {
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:[UIImage imageNamed:@"baseImg_common_close"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(onCancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

@end
